package com.ciwong.epaper.modules.cordva.html;


import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.evaluate.bean.ShareBean;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.share.WxShareHelper;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.widget.ShareDialog;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.i.GoBackListener;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.net.URLEncoder;

/**
 * 单词PK小游戏
 */

public class PkActivity extends BaseHtmlActicity {
    private ShareDialog shareDialog;

    @Override
    protected void preCreate() {
        super.preCreate();
        try {
            //去除title
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            //去掉Activity上面的状态栏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    @Override
    protected void init() {
        super.init();
        setValideSource(false);
        hideTitleBar();
        //setBackImg(R.mipmap.close);
        EventBus.getDefault().register(this);
        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                finish();
            }
        });
    }

    @Override
    protected void findViews() {
        super.findViews();
        findViewById(R.id.pk_title).setVisibility(View.VISIBLE);
        findViewById(R.id.pk_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void goBack() {
        finish();
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            finish();
            return true;
        }
        return false;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {


        int keyCode = event.getKeyCode();
        boolean isBackButton = keyCode == KeyEvent.KEYCODE_BACK;
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (isBackButton) {
                if (shareDialog != null && shareDialog.isShowing()) {
                    return super.dispatchKeyEvent(event);
                } else {
                    return true;
                }

            } else {
                return super.dispatchKeyEvent(event);
            }
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            if (isBackButton) {

                if (shareDialog != null && shareDialog.isShowing()) {
                    shareDialog.dismiss();
                    return super.dispatchKeyEvent(event);
                } else {
                    finish();
                    return true;
                }

            } else {
                return super.dispatchKeyEvent(event);

            }

        } else {
            return super.dispatchKeyEvent(event);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        WxShareHelper.getInstance().onResumeResultData();
    }

    @Override
    protected void loadData() {
        super.loadData();
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {

            String clientId = verifyInfo.getClientId();
            String userId = EApplication.getInstance().getUserInfoBase().getUserId() + "";

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("school", EApplication.getInstance().getSchoolDetail().getSchoolName());
                jsonObject.put("headimgurl", EApplication.getInstance().getUserInfoBase().getAvatar());
                jsonObject.put("nickname", EApplication.getInstance().getUserInfoBase().getRealName());
                jsonObject.put("class", EApplication.getInstance().getClazz().getClassName());
                jsonObject.put("openid", userId);

            } catch (Exception e) {
                e.getStackTrace();
            }


            String url = EpaperConstant.URL_WORD_PK_H5 + "clientId=" + clientId + "&userinf=";
            String p = jsonObject.toString();

            try {
                //url=URLEncoder.encode(url);
                p = URLEncoder.encode(p, "UTF-8");
            } catch (Exception e) {
                e.getStackTrace();
            }
            url = url + p;
            setStartURL(url);
            loadUrl(startURL.toString());
        } else {
            showToastError(getString(R.string.get_user_info_error));
            finish();
        }

    }

    Gson gson = new Gson();

    public void onEventMainThread(SubmitEvent submitEvent) {
        if (submitEvent.getId().equals("GetShareInfo")) { //标题栏显示分享按钮
            showShareBtn(submitEvent);
        } else if (submitEvent.getId().equals("DisplayShare")) {//是否展示分享按钮
//            MeDao.getInstance().getUserPoint(EApplication.getInstance().getUserInfoBase().getUserId() + "");
        } else if (submitEvent.getId().equals("IsFirstPK")) {//第一次pk加积分
            MeDao.getInstance().getUserPoint();
        }
    }

    private void showShareBtn(final SubmitEvent submitEvent) {
        ShareBean shareBean = gson.fromJson(submitEvent.getJson(), ShareBean.class);
        shareBean.moduleId = 1;
        shareBean.setTitle("单词PK");
        shareDialog = new ShareDialog(PkActivity.this, shareBean);
        shareDialog.setmQQFrGone(true);
//        shareDialog.setParamForAddPoint(null, AddPointTypes.SHERE_RANK);//添加分享加积分

//                        toastInCenter("doworkID:"+shareBean.getDoWorkId());
//                        System.out.println("doworkID:"+shareBean.getDoWorkId());
        shareDialog.show();

    }


}
